﻿using System;
using System.Collections.Generic;
using System.Linq;
using ESoft.Core.Data.Service;
using ESoft.Core.Utils.Extensions;
using ESoft.Core.Web.Common.Filters;
using ESoft.Core.Web.Utils;
using ESoft.Core.Web.VO;
using ESoft.Portal.Areas.ContentManager.Data.Service;
using ESoft.Portal.Data.Domain.Entity;
using ESoft.Portal.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ESoft.Portal.ApiControllers
{
    [Route("api/site/article")]
    [ApiController]
    [SkipUserAuthorize]
    public class ArticleController : ControllerBase
    {
        private readonly IBaseService _baseService;
        private readonly IChannelService _channelService;

        public ArticleController(IBaseService baseService, IChannelService channelService)
        {
            _baseService = baseService;
            _channelService = channelService;
        }

        [Route("page")]
        [HttpGet]
        public object GetPageList([FromQuery]PageInput pageInput, [FromQuery]string keyword, [FromQuery]string channelId, [FromQuery]int? isHot)
        {
            var channelList = new List<string> { };
            if (channelId.IsNotNullOrWhiteSpace())
            {
                channelList = _channelService.GetAllChannel(channelId).Select(x => x.ChannelId).ToList();
                channelList.Add(channelId);
            }

            List<Dictionary<string, object>> articleList = _baseService.QueryPageEntities<CContentArticleEntity>(pageInput,
                    x => (keyword.IsNullOrEmpty() || x.Title.Contains(keyword))
                         && (channelList.Count == 0 || x.ChannelId.In(channelList))
                         && (isHot == null || x.IsHot == isHot),

                    new SortVo { SortField = "CreateDatetime", SortWay = false })
                    .Select(x => new Dictionary<string, object>
                    {
                        { "articleId",x.ArticleId},
                        { "title",x.Title},
                        { "channelId",x.ChannelId},
                        { "channelName",""},
                        { "channelType",0},
                        { "isHot",x.IsHot},
                        { "imageId",x.ImageId},
                        { "sort",x.Sort},
                        { "url",x.Url},
                        { "createDatetime",x.CreateDatetime},
                        { "richTextId",x.RichTextId},
                        { "content",""},
                    }).ToList();

            foreach (var article in articleList.Where(article => article["channelId"].IsNotNull() &&
                                                      article["channelId"].ToString().IsNotNullOrWhiteSpace()))
            {
                var cc = _baseService.Find<CContentChannelEntity>(article["channelId"]);
                if (cc != null)
                    article["channelName"] = cc.ChannelName;

                var richText = _baseService.Find<CRichTextEntity>(article["richTextId"]);
                if (richText != null)
                    article["content"] = StringUtils.ReplaceHtmlTag(System.Text.Encoding.Default.GetString(richText.TextContent));
            }

            var count = _baseService.Count<CContentArticleEntity>(x => (keyword.IsNullOrEmpty() || x.Title.Contains(keyword))
                                                                       && (channelList.Count == 0 || x.ChannelId.In(channelList))
                                                                       && (isHot == null || x.IsHot == isHot));

            return ResponseResult.Success(new PageResult<Dictionary<string, object>>(articleList, count));
        }

        [Route("{articleId}")]
        [HttpGet]
        public object Get([FromRoute]string articleId)
        {
            var article = _baseService.Find<CContentArticleEntity>(articleId);


            var richText = _baseService.Find<CRichTextEntity>(article.RichTextId);
            var result = new Dictionary<String, object>
            {
                { "articleId",article.ArticleId},
                { "title",article.Title},
                { "channelId",article.ChannelId},
                { "channelName",""},
                { "channelImageId",null},
                { "channelType",null},
                { "isHot",article.IsHot},
                { "imageId",article.ImageId},
                { "sort",article.Sort},
             
            { "createDatetime",article.CreateDatetime},
                { "url",article.Url},
                {"fullDetail", System.Text.Encoding.Default.GetString(richText.TextContent) },
            };
            if (article.ChannelId.IsNotNullOrWhiteSpace())
            {
                var channel = _baseService.Find<CContentChannelEntity>(article.ChannelId);
                if (channel.IsNotNull())
                {
                    result["channelName"] = channel.ChannelName;
                    result["channelType"] = channel.ChannelType;
                    result["channelImageId"] = channel.ImageId;
                }
            }
            return ResponseResult.Success(result);
        }
    }
}